﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

using Util;

namespace Algorithm.Sort
{
    public class SelectionSorter <T> : SorterBase <T>
    {
        [AlgorithmVariation]
        private static void SelectionSort(T[] list, IComparer<T> comparer)
        {
            int count = list.Length;

            // Index of the minimum element in each loop.
            int minimumIndex;

            // The minimum element one in each loop.
            T temp;

            // Each loop selects the minimum element and put it in order.
            for (int i = 0; i < count; i++)
            {
                minimumIndex = i;
                temp = list[i];

                // Finds the minimum element in rest elements.
                for (int j = i + 1; j < count; j++)
                {
                    if (comparer.Compare(list[j], temp) < 0)
                    {
                        minimumIndex = j;
                        temp = list[j];
                    }
                }

                Swap(list, i, minimumIndex);
            }
        }
    }
}
